package com.xiaowu.house.user.controller;

import com.xiaowu.house.appuser.domain.dto.AppUserDTO;
import com.xiaowu.house.appuser.domain.dto.AppUserListReqDTO;
import com.xiaowu.house.appuser.domain.dto.UserEditReqDTO;
import com.xiaowu.house.appuser.domain.vo.AppUserVo;
import com.xiaowu.house.appuser.feign.AppUserFeignClient;
import com.xiaowu.house.domain.R;
import com.xiaowu.house.domain.dto.BasePageDTO;
import com.xiaowu.house.domain.vo.BasePageVO;
import com.xiaowu.house.user.service.IAppUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


/**
 * C端用户相关接口
 */
@RestController
@RequestMapping("/app_user")
public class AppUserController implements AppUserFeignClient {

    @Autowired
    IAppUserService appUserService;


    /**
     * 根据微信注册用户
     * @param openId 用户微信ID
     * @return C端用户VO
     */
    @Override
    public R<AppUserVo> registerByOpenId(String openId) {
        return R.ok(appUserService.registerByOpenId(openId).convertToVO());
    }

    /**
     * 根据openId查询用户信息
     * @param openId 用户微信ID
     * @return C端用户VO
     */
    @Override
    public R<AppUserVo> findByOpenId(String openId) {
        return R.ok(appUserService.findByOpenId(openId).convertToVO());
    }

    @Override
    public R<AppUserVo> findByPhone(String phoneNumber) {
        AppUserDTO appUserDTO = appUserService.findByPhone(phoneNumber);
        if (appUserDTO == null) {
            return R.ok(null);
        }
        return R.ok(appUserDTO.convertToVO());
    }

    @Override
    public R<AppUserVo> registerByPhone(String phoneNumber) {
        return R.ok(appUserService.registerByPhone(phoneNumber).convertToVO());
    }

    @Override
    public R<Void> edit(UserEditReqDTO userEditReqDTO) {
        appUserService.edit(userEditReqDTO);
        return R.ok();
    }

    @Override
    public R<AppUserVo> findById(Long userId) {
        return R.ok(appUserService.findById(userId).convertToVO());
    }

    @Override
    public R<List<AppUserVo>> list(List<Long> userIds) {
        return R.ok(appUserService.list(userIds)
                .stream()
                .map(AppUserDTO::convertToVO)
                .collect(Collectors.toList()));
    }


    /**
     * 查询C端用户
     * @param appUserListReqDTO 查询C端用户DTO
     * @return C端用户分页结果
     */
    @PostMapping("/list/search")
    public R<BasePageVO<AppUserVo>> list(@RequestBody AppUserListReqDTO appUserListReqDTO) {
        BasePageDTO<AppUserDTO> appUserDTOList = appUserService.getUserList(appUserListReqDTO);
        BasePageVO<AppUserVo> result = new BasePageVO<>();
        BeanUtils.copyProperties(appUserDTOList, result);
        return R.ok(result);
    }
}
